#include "processmanager.h"
#include <QtDebug>

ProcessManager::ProcessManager(QObject *parent) : QObject(parent)
{
    connect(&proCtrl, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(proCtrlStateChanged(QProcess::ProcessState)));
    connect(&proCtrl, SIGNAL(error(QProcess::ProcessError)), this, SLOT(proCtrlError(QProcess::ProcessError)));
    connect(&proCtrl, SIGNAL(readyRead()), this, SLOT(proCtrlMsg()));
}

void ProcessManager::proCtrlStateChanged(QProcess::ProcessState state)
{
    qDebug()<<"[ProCtrl] process state changed:"<<state;
}

void ProcessManager::proCtrlError(QProcess::ProcessError err)
{
    qDebug()<<"[ProCtrl] error:"<<err;
}

void ProcessManager::proCtrlMsg()
{
    qDebug()<<"[ProCtrl]"<<proCtrl.readAll();
}

void ProcessManager::startProCtrl()
{
    if(proCtrl.state() == QProcess::NotRunning)
    {
        proCtrl.setWorkingDirectory("/home/hx/v2/");
//        proCtrl.setProgram("/home/hx/v2/auto");
        qDebug()<<"[ProCtrl] process start.";
        proCtrl.start("sudo ./auto");
    }
}

void ProcessManager::stopProCtrl()
{
//    if(proCtrl.state() == QProcess::Running)
    {
        qDebug()<<"[ProCtrl] process kill.";
        QProcess proKill;
        proKill.startDetached("sudo killall auto");
    }
}
